<?php
class Default_Model_Module {

    private $_arrayData ;//= array('Administrator'=>'系統管理者','Guest'=>'訪客','Register'=>'已註冊會員','Parent'=>'家長','Student'=>'學生','Teacher'=>'教師');

    function __construct($options=null){
        $locale = Bootstrap::$config['resources']['locale']['default'];
        $path = dirname(__FILE__)."/../languages/$locale/data.ini";
        $ini = new Zend_Config_Ini($path);
        $this->_arrayData = $ini->get('array_data')->toArray();
//        $this->_role_kind = $this->_arrayData['role_kind'];
//        $this->_system_roles = $this->_arrayData['system_roles'];
    }

    /**
     * 授權項目
     * @return unknown_type
     */
    function getRoleKind()
    {
        return $this->_arrayData['role_kind'];
    }

    /**
     * 分類選單
     * @return array
     */
    function getModuleMenu($id='', $topTitle='')
    {
        $resourceView = new Default_Model_AclResourcesView();
        return $resourceView->getModuleMenu($id, $topTitle);
    }

    /**
     * 未安裝模組
     * @return array
     */

    function getYetInstallModule()
    {
        $resource = new Default_Model_Resources();
        $arr = $resource->getTable()->findBySql('kind=?','module')->toArray();
        //		$resource = new Default_Model_DbTable_Resources();
        //		$arr = $resource->fetchAll("kind='module'")->toArray();
        $rarr = array ();
        foreach ($arr as $row)
        $rarr[]=$row['id'];

        $module_path = APPLICATION_PATH.'/modules';
        $arr = Sfs_Filesystem_Dir::getDirectories($module_path);
        $noin_arr = array();
        $all_modules = array();
        foreach($arr as $res) {
            if (!in_array($res,$rarr)) {
                $translate = Zend_Registry::get('Zend_Translate');
                $moduleXml = $module_path.'/'.$res.'/module.xml';
                if (is_file($moduleXml)) {
                    $trName = APPLICATION_PATH.'/modules/'.$res.'/languages/'.
                    $translate->getLocale().'/module-cfg.php';
                    if (is_file($trName)){
                        $translate->addTranslation($trName);
                    }
                      $result  = new Zend_Config_Xml($moduleXml);
                    $all_modules[] = $result->toArray();
                }
            }
        }
        return $all_modules;
    }

    /**
     * 回傳授權項目
     * @param $id
     * @return array
     */
    function getPerrItem($id)
    {
       // $db = Zend_Db_Table::getDefaultAdapter();
        switch($id){
            case Default_Model_AclRoles::ROLE_ROOM:  // 處室
                $q = Doctrine_Query::create()
                ->select('s.name AS sn, s.name')
                ->from('School_Model_SchoolRoom s')
                ->leftJoin('s.Default_Model_AclRoles a')
                ->orderby('s.rank');
                $res = $q->fetchArray();
                $res[]=array('sn'=>'Teacher','name'=>$this->_arrayData['AllTeacher']['name']);
                return $res;
                break;
            case Default_Model_AclRoles::ROLE_TITLE: // 職稱
                $q = Doctrine_Query::create()
                ->select('t.name A sn, t.name')
                ->from('Teacher_Model_TeacherTitle t')
                ->leftjoin('t.Default_Model_AclRoles a')
                ->orderBy('t.sort');

                $res = $q->fetchArray();
                return $res;
                break;
            case Default_Model_AclRoles::ROLE_DEFAULT : //系統群組
                $res = array();
                foreach($this->_arrayData['system_roles'] as $id=>$val){
                    $res[]=array('sn'=>$id,'name'=>$val);
                }
                return $res;
                break;
                 case Default_Model_AclRoles::ROLE_CUSTOM : //自訂群組
                $res = array();
                 $q = Doctrine_Query::create()
                ->from('Default_Model_AclRoles')
                ->where('role_kind=?' , Default_Model_AclRoles::ROLE_CUSTOM)
                ->fetchArray();
                foreach($q as $row){
                    $res[]=array('sn'=>$row['id'],'name'=>$row['id']);
                }
                return $res;
                break;

        }

    }
}
